﻿using System;
using System.IO;
using Microsoft.Xna.Framework;
using Ultima.Xna.Scripting;

namespace Ultima.Xna.Graphics.UI.Gumps
{    
    public class Gump : UINode
    {
        private LuaEngine _luaEngine;
        private string _scriptFile;
        private int _typeID;
        private bool _dragable = true;
        private bool _closable = true;
        private bool _resizable = true;
        private bool _disposable = true;

        public string ScriptFile
        {
            get { return _scriptFile; }
            set { _scriptFile = value; }
        }

        public int TypeID
        {
            get { return _typeID; }
            set { _typeID = value; }
        }

        public bool Dragable
        {
            get { return _dragable; }
            set { _dragable = value; }
        }

        public bool Closable
        {
            get { return _closable; }
            set { _closable = value; }
        }

        public bool Resizable
        {
            get { return _resizable; }
            set { _resizable = value; }
        }

        public bool Disposable
        {
            get { return _disposable; }
            set { _disposable = value; }
        }

        public override Vector2 Position
        {
            get { return Vector2.Zero; }
            set { base.Position = Vector2.Zero; }
        }

        public override Vector2 Size
        {
            get
            {
                return new Vector2(Game.GraphicsDevice.PresentationParameters.BackBufferWidth,
                    Game.GraphicsDevice.PresentationParameters.BackBufferHeight);
            }
            set
            {
                base.Size = new Vector2(Game.GraphicsDevice.PresentationParameters.BackBufferWidth,
                  Game.GraphicsDevice.PresentationParameters.BackBufferHeight); 
            }
        }

        public override int Width
        {
            get { return Game.GraphicsDevice.PresentationParameters.BackBufferWidth; }
            set { base.Width = Game.GraphicsDevice.PresentationParameters.BackBufferWidth; }
        }

        public override int Height
        {
            get { return Game.GraphicsDevice.PresentationParameters.BackBufferHeight; }
            set { base.Height = Game.GraphicsDevice.PresentationParameters.BackBufferHeight; }
        }

        public override int X
        {
            get { return 0; }
            set { base.X = 0; }
        }

        public override int Y
        {
            get { return 0; }
            set { base.Y = 0; }
        }

        public override Rectangle Bounds
        {
            get { return new Rectangle(X, Y, Width, Height); }
            set { base.Bounds = new Rectangle(X, Y, Width, Height); }
        }

        public override Vector2 RenderPosition
        {
            get { return Vector2.Zero; }
        }

        public override Vector2 RenderSize
        {
            get
            {
                return new Vector2(Game.GraphicsDevice.PresentationParameters.BackBufferWidth,
                    Game.GraphicsDevice.PresentationParameters.BackBufferHeight);
            }
        }

        public override Rectangle RenderBounds
        {
            get { return new Rectangle(X, Y, Width, Height); }
        }

        public Gump(Game game)
            : base(game)
        {
            _luaEngine = new LuaEngine();
        }

        public override void Initialize()
        {
            base.Initialize();

            if (File.Exists(_scriptFile))
            {
#if DEBUG
                _luaEngine.Lua.DebugHook +=new EventHandler<DebugHookEventArgs>(OnLuaDebugHook);
#endif
                _luaEngine.RegisterLuaFunctions(this);
                _luaEngine.Lua.LoadFile(_scriptFile);
            }
        }

        private void OnLuaDebugHook(object sender, DebugHookEventArgs e)
        {
            Manager.Log.Debug(e.LuaDebug.ToString());
        }

        public override void Invalidate()
        {
            
        }

        public override bool HitTest(Vector2 position)
        {
            return FindFirstChildAt(position) != null;
        }
    }
}
